package demo150;

public class trans {
    public String trans (String s, int n) {
        // write code here
        StringBuffer ret = new StringBuffer();

        int len = s.length()-1;
        int flg = len;
        int i = len;
        for(;i >=0;i--) {
            if(s.charAt(i)==' '){
                int j = i+1;
                while(j <= flg){
                    if(s.charAt(j) >= 'a' &&s.charAt(j) <='z'){
                        ret.append(Character.toUpperCase(s.charAt(j)));
                    }else {
                        ret.append(Character.toLowerCase(s.charAt(j)));
                    }
                    j++;
                }
                if(i > 0){
                    ret.append(s.charAt(i));
                    flg = i-1;
                }
            }
        }
        if(i < 0 && s.charAt(i+1) !=' '){
            int j = i+1;
            while(j <= flg){
                if(s.charAt(j) >= 'a' &&s.charAt(j) <='z'){
                    ret.append(Character.toUpperCase(s.charAt(j)));
                }else {
                    ret.append(Character.toLowerCase(s.charAt(j)));
                }
                j++;
            }
        }else {
            ret.append(s.charAt(i+1));
        }
        return ret.toString();
    }
}
